package py.com.alfa.escriba.web.services.impl;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;

import java.util.List;

import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.persistence.dao.VariablesDAO;
import py.com.alfa.escriba.persistence.dto.VariablesDTO;
import py.com.alfa.escriba.util.Utils;
import py.com.alfa.escriba.web.interceptor.InvocationContext;
import py.com.alfa.escriba.web.services.VariablesService;

public class VariablesServiceImpl implements VariablesService{

  private VariablesDAO variablesDAO;

  public void setVariablesDAO(VariablesDAO variablesDAO) {
    this.variablesDAO = variablesDAO;
  }

  @Override
  public List<VariablesDTO> listVariables(InvocationContext ic) throws ServiceException {
    List<VariablesDTO> variabless =  variablesDAO.listVariables();

    return variabless;
  }

  @Override
  public VariablesDTO getVariables(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    VariablesDTO variables =  variablesDAO.getVariablesById(id);

    if (variables == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"variables.notfound", id);

    return variables;
  }

  @Override
  public void deleteVariables(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    VariablesDTO variables =  variablesDAO.getVariablesById(id);

    if(variables == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"variables.notfound", id);

    try {
      variablesDAO.deleteVariables(variables);
    }
    catch (DataIntegrityViolationException e){
      throw new ServiceException(ErrorCode.DATA_INTEGRITY_VIOLATION, "variables.constraint.violation", id);
    }

  }

  @Override
  public VariablesDTO updateVariables(Long id, VariablesDTO dto, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    if (null == dto)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    VariablesDTO variables = variablesDAO.getVariablesById(id);

    if (variables == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"variables.notfound", id);

    variables.setCodigo(dto.getCodigo());
    variables.setNombre(dto.getNombre());
    variables.setCampo(dto.getCampo());
    variables.setIdtipoobjeto(dto.getIdtipoobjeto());
    variables.setIstemplate(dto.getIstemplate());
    variables.setTexto(dto.getTexto());
    variables.setIdtemplate(dto.getIdtemplate());
    variables.setIsrepetitivo(dto.getIsrepetitivo());
    variables.setFormato(dto.getFormato());
    variables.setVisibilidad(dto.getVisibilidad());
    variables.setObligatoriedad(dto.getObligatoriedad());
 

    Utils.validarDTO(dto);

    variablesDAO.updateVariables(dto);

    return variables;
  }

  @Override
  public VariablesDTO createVariables(VariablesDTO dto, InvocationContext ic) throws ServiceException {
    if (dto == null)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    try {

      Utils.validarDTO(dto);

      variablesDAO.createVariables(dto);
    }
    catch (DuplicateKeyException e){
      throw new ServiceException(ErrorCode.DUPLICATE_KEY_EXCEPTION, "variables.duplicate.key");
    }

    return dto;
  }

}